/**
 * 
 */
package rai.dao;

import org.apache.log4j.Logger;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import rai.form.RegForm;
import rai.util.Constants;

/**
 * @author Tien+Vu
 * 
 */
public class RegFormDAO {
    private static Logger log = Logger.getLogger("AddData");

    public boolean addData(RegForm regform) {
        Connection conn = null;
        String query = Constants.BLANK_VALUE;
        int rs;
        boolean succ = false;
        PreparedStatement pStat = null;
        
        try {
            query = "insert into user (UserName,PassWord,"
            		+ "LastLogin,IsDeleted)";
            conn = rai.util.Connection.getConnection();
            query += "values('" + regform.getUserName() + "','" + regform.getPassword()
                    + "'," + null + "," + 0 + ")";
            pStat = conn.prepareStatement(query);
            rs = pStat.executeUpdate();
            if (rs == 1) {
                query = "insert into employee (Name, Birthday, Address, Email, "
                       + "Phone, Gender, DepartID, IsDeleted) "
                       + "values('" + regform.getTxtEmpName() + "','" 
                       + regform.getTxtBirthday() + "','" + regform.getTxtAddress()
                       + "','" + regform.getTxtEmail() + "','" + regform.getTxtPhone()
                       + "'," + Integer.toBinaryString(regform.getRadioGender())
                       + ",'" + regform.getLstDept()
                       + "',0)";
                pStat = conn.prepareStatement(query);
                rs = pStat.executeUpdate();
                if (rs == 1) {
                    succ = true;
                }
            }
        } catch (Exception sqlEx) {
            log.error("AddData.getUser " + sqlEx);
        } finally {
            try {
                pStat.close();
                conn.close();
            } catch (SQLException sqlEx) {
                log.error("AddData.getUser " + sqlEx);
            }
        }
        return succ;
    }

}
